package cn.progen.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class MapToObj{
	public  static Object MapToObj(List<Map<String,Object>> map,Class class1){			
			List rs=new ArrayList();
			map.forEach(item->{
				Object s;
				try {
					s = class1.newInstance();				
					for (Entry<String, Object> entry : item.entrySet()) { 
					    String name=entry.getKey();
					    Object values=entry.getValue();
						Field field;					
						try {
							//field = class1.getDeclaredField(name);		
							Method m1=class1.getDeclaredMethod("set"+StringUtil.firstLetterToUpper(name), String.class);

							m1.invoke(s,values);
							//field.setAccessible(true);
						    //field.set(s, values);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							System.out.println("set"+StringUtil.firstLetterToUpper(name)+"::::"+values);
						}						
					}
					rs.add(s);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			});
			return rs;	 
		
	}

	public  static Object MapToObj(Map<String,Object> item,Class class1){
			Object s=null;
			try {
				s = class1.newInstance();
				for (Entry<String, Object> entry : item.entrySet()) {
					String name=entry.getKey();
					Object values=entry.getValue();
					Field field;
					try {
						//field = class1.getDeclaredField(name);
						Method m1=class1.getDeclaredMethod("set"+StringUtil.firstLetterToUpper(name), String.class);
						m1.invoke(s,values);
						//field.setAccessible(true);
						//field.set(s, values);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		return s;

	}
}
